package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BELado;
import org.rx.cr.dao.DAOAbstract;

public class DALado extends DAOAbstract<BELado> {

    public DALado(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BELado be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_lado");
            setParameterString(be.getNombre());
            setParameterDouble(be.getMedida());
            setParameterDouble(be.getValorunitario());
            setParameterInt(be.getTerrenoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BELado be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_lado");
            setParameterInt(be.getLadoid());
            setParameterString(be.getNombre());
            setParameterDouble(be.getMedida());
            setParameterDouble(be.getValorunitario());
            setParameterInt(be.getTerrenoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BELado be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_del_lado");
            setParameterInt(be.getLadoid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BELado> buscarReferencia(String ref) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_lado");
            setParameterString(ref);
            commitc();
            while (existResult()) {
                BELado be = new BELado();
                be.setLadoid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setMedida(getDataDouble(3));
                be.setValorunitario(getDataDouble(4));
                be.setTerrenoid(getDataInt(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BELado> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_lado");
            setParameterString("");
            commitc();
            while (existResult()) {
                BELado be = new BELado();
                be.setLadoid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setMedida(getDataDouble(3));
                be.setValorunitario(getDataDouble(4));
                be.setTerrenoid(getDataInt(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BELado buscar(int i) throws Exception {
        try {
            BELado be =null;
            
            setStoreProcedure("SISVAL.sp_sel_lado_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BELado();
                be.setLadoid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setMedida(getDataDouble(3));
                be.setValorunitario(getDataDouble(4));
                be.setTerrenoid(getDataInt(5));
                add(be);
            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
